package com.hw.dao.utils;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.hw.dagl.model.Sys_Role;
import com.hw.utils.StringUtils;
@Repository
public class OraclePageDaoBase extends JdbcTemplate {
	private Logger log = LoggerFactory.getLogger(this.getClass());
	@Resource(name="dataSource")
	private DataSource dataSource;
	
	/*@Resource(name="dataSource2")
    private DataSource dataSource2;*/
	
    /**
	 * 分页查询记录
	 * @param sql
	 * @param pages
	 * @param rowMapper
	 * @param args
	 * @return
	 */
	public <X> Page<X> queryForPage(String sql,Page<X> pages,RowMapper<X> rowMapper,Object...args){
		int minNum = pages.getFirst();
		int maxNum = minNum + pages.getPageSize();
		StringBuffer pageSQL = new StringBuffer("SELECT * FROM (SELECT pagesql_TTT.*, ROWNUM RN FROM ("+sql+") pagesql_TTT WHERE ROWNUM < "+maxNum+") WHERE RN >= "+minNum);
		log.info("SQL:"+pageSQL.toString());
		for (Object object : args) {
			log.info("参数:"+object);
		}
		List<X> resultList = super.query(pageSQL.toString(), args, rowMapper);
		pages.setRows(resultList);
		if(pages.getTotal() ==-1){
			pages.setTotal(this.findCountTotal(sql, args));
		}
		
		return pages;
	}
	/**
	 * 分页查询记录
	 * @param sql
	 * @param pages
	 * @param rowMapper
	 * @param args
	 * @return
	 */
	public <X> GcPage<X> queryForGcPage(String sql,GcPage<X> pages,RowMapper<X> rowMapper,Object...args){
		int minNum = pages.getFirst();
		int maxNum = minNum + pages.getPageSize();
		StringBuffer pageSQL = new StringBuffer("SELECT * FROM (SELECT pagesql_TTT.*, ROWNUM RN FROM ("+sql+") pagesql_TTT  WHERE ROWNUM < "+maxNum+") WHERE RN >= "+minNum);
		log.info("SQL:"+pageSQL.toString());
		for (Object object : args) {
			log.info("参数:"+object);
		}
		List<X> resultList = super.query(pageSQL.toString(), args, rowMapper);
		pages.setRows(resultList);
		if(pages.getTotal() ==-1){
			pages.setTotal(this.findCountTotal(sql, args));
		}
		
		return pages;
	}
	/**
	 * 查询记录总数
	 * @param sql
	 * @param args
	 * @return
	 */
	private int findCountTotal(String sql,Object...args){
		//判断当前是否有总记录数，如果没有，则查询一次总记录数
		String selectCountSQL = "select count(1) from ("+sql+") ";
		return super.queryForInt(selectCountSQL, args);
	}
	
	/**
	 * 分页查询记录--返回分页sql和total总记录数
	 * @param sql
	 * @param pages
	 * @param args
	 * @return
	 */
	public <X> Map<String,String> getPageSQLAndTotal(String sql,Page<X> pages,Object...args){
		int minNum = pages.getFirst();
		int maxNum = minNum + pages.getPageSize();
		if(StringUtils.isNotEmpty(pages.getOrderBy())){
			sql = new StringBuffer(" SELECT pagesql_TTT_order.* FROM ("+sql+") pagesql_TTT_order ORDER BY "+pages.getOrderBy()+" "+pages.getOrder()).toString();
		}
		StringBuffer pageSQL = new StringBuffer("SELECT * FROM (SELECT pagesql_TTT.*, ROWNUM RN FROM ("+sql+") pagesql_TTT WHERE ROWNUM < "+maxNum+") WHERE RN >= "+minNum);
		long total =0L;
		if(pages.getTotal() ==-1){
			total = this.findCountTotal(sql, args);
		}
		Map<String,String> map = new HashMap<String,String>();
		map.put("pageSQL",pageSQL.toString());
		map.put("total",String.valueOf(total));
		return map;
	}
	
	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
	     this.dataSource = dataSource;
	}
	
/*	 public DataSource getDataSource2() {
	     return dataSource2;
    }
    public void setDataSource2(DataSource dataSource2) {
        this.dataSource2 = dataSource2;
    }
	
	public void setSourceByRole(Sys_Role sysRole){
	    String rid=sysRole.getRid();
	    if("43".equals(rid)){
	        setDataSource(dataSource2);
        }else{
            setDataSource(dataSource);
        }
	}*/

	
	
}
